app.register.controller('PartsInvoStatisticCtrl', function($scope, $http, $rootScope,
		$route, PartsInvoStatisticSer, AppUtil, $q,$routeParams) {
	
	loadFlag($routeParams.id,$scope);
	$scope.selected={};
	var date = new Date();
    var nowYear = date.getFullYear();
    var month=date.getMonth()+1>=10?date.getMonth()+1:'0'+(date.getMonth()+1);
	$("#report_year").val(nowYear);
	$scope.selected.report_month=month;
	$("#report_month").val(month);
	$scope.pagination = {
			currentPage : 1,
			itemsPerPage : 10
	};
	
	$scope.SearchData = function() {
		if(!$("#report_year").val()){
			alert("请选择年份");
			return;
		}
		$scope.postData={};
		// 点击查询按钮时,恢复页码为1
		$scope.pagination.currentPage = 1
		// 表单查询参数
		LoadList();
	};
	
	
	
	
	// 加载数据方法
	var LoadList = function() {
		if ($scope.postData == undefined)
			$scope.postData = {};
		var params = AppUtil.Params(".form_params");
		params = $.extend({}, $scope.selected,params);
		// 当有查询参数时,重置页数
		if (AppUtil.Length(params) > 0) {
			$scope.postData.params = params;
		}
		// 筛选查询参数
		var search_opt = AppUtil.GetSearchOpt();
		if (search_opt.length > 0) {
			$scope.postData.search_opt = search_opt;
		}
		$scope.postData.page = $scope.pagination.currentPage;
		$scope.postData.size = $scope.pagination.itemsPerPage;
		// 加载数据
		PartsInvoStatisticSer.list($scope.postData).success(function(response) {
			AppUtil.remove_loading();
			$scope.pagination.totalItems = response.total;
			$scope.partsInvo = response.data.data;
			$scope.partsInvoSum = response.data.sum;
			setTimeout(check_table_tr,500);
		});
	}
	// 配置分页基本参数
	$scope.$watch('pagination.refresh',LoadList);

	
	//加载下拉列表数据
	$http.get("partsStatistics/dicts").success(function(data){
		$scope.storage_list=data.data.storage_list;
		$scope.pre_storage_list=data.data.storage_list;
		$("#storageCodeName").html("");
		$.each($scope.storage_list,function(i,o){
			if($scope.selected==undefined||$scope.selected.storage_name==undefined)
				$("#storageCodeName").append("<option value='"+o.storage_code+"'>"+o.storage_name+"</option>")
			else{
				var storage_name = $scope.selected.storage_name.split(",");
				
				if($.inArray(o.storage_name, storage_name)!=-1){
					$("#storageCodeName").append("<option selected='selected' value='"+o.storage_code+"'>"+o.storage_name+"</option>");
				}else
					$("#storageCodeName").append("<option value='"+o.storage_code+"'>"+o.storage_name+"</option>");
			}
		})
		$("#storageCodeName").multiselect({ 
			noneSelectedText: "==请选择==", 
			checkAllText: "全选", 
			uncheckAllText: '全不选', 
			selectedList:2,
			minWidth:"100%"
	    });
	});
	
	
	$scope.LoadEntityLevel = function(entity) {
		//加载下拉列表数据
		$http.get("partsStatistics/dicts?entityCode="+entity.entity_code).success(function(data){
			$("#storageCodeName").html("");
			$scope.storage_list=data.data.storage_list;
			$.each($scope.storage_list,function(i,o){
					$("#storageCodeName").append("<option value='"+o.storage_code+"'>"+o.storage_name+"</option>")
			})
			$("#storageCodeName").multiselect({ 
				noneSelectedText: "==请选择==", 
				checkAllText: "全选", 
				uncheckAllText: '全不选', 
				selectedList:2,
				minWidth:"100%"
		    });
			$("#storageCodeName").multiselect('refresh');
		})
	}
	
	//清空门店
	$scope.clearGroupBrandEntity = function(){
		$scope.storage_list=$scope.pre_storage_list;
		$("#storageCodeName").html("");
		$.each($scope.storage_list,function(i,o){
				$("#storageCodeName").append("<option value='"+o.storage_code+"'>"+o.storage_name+"</option>")
		})
		$("#storageCodeName").multiselect({ 
			noneSelectedText: "==请选择==", 
			checkAllText: "全选", 
			uncheckAllText: '全不选', 
			selectedList:2,
			minWidth:"100%"
	    });
		$("#storageCodeName").multiselect('refresh');
	}
	
	// 加载品牌
	$http.get("common/brands").success(function(data) {
		$scope.brands = data;
	});
	
	// 加载门店
	AppUtil.LoadGroups();
	
	// 导出
	$scope.ExportData = function() {
		AppUtil.ExportData($scope.postData);
	};
});

app.register.service('PartsInvoStatisticSer', function($http,AppUtil) {
	this.list = function(postData) {
		AppUtil.loading();
		//设置查询开始下标
		postData.start= (postData.page-1<0?0:postData.page-1)*postData.size;
		return $http.post('partsStatistics/list', postData).error(function() {
			AppUtil.remove_loading();
		});;
	};

});
